/**
* This file is part of mobileworks.
*
* mobileworks is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with dev.clipall.  If not, see <http://www.gnu.org/licenses/>.
*
* https://code.google.com/p/mobileworks/
*/

/*
 * Main.java
 * Created on May 3, 2008, 3:44 PM
 */

package fontmaker;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Erol Hira
 */
public class Main {
    
    public Main() {
    }
    
    
    public static void main(String[] args) {
        try {
            generateFontPropDataFile(args[0], args[1], args[2]);
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     *  1 byte : font count
     *  for each font:
     *      1 byte : char count
     *      n bytes : widths
     *      n chars : chars
     */
    public static void generateFontPropDataFile(String dir, String constantsFile, String fontDataFile) throws Exception{        
        
        File[] files = new File(dir).listFiles();
        
        String[] fontNames = new String[files.length];
        byte[][] widthsArray = new byte[files.length][];
        char[][] charsArray = new char[files.length][];
        
        for(int i = 0; i < files.length; i++){
            
            String fileName = files[i].getName();
            fontNames[i] = fileName.substring(0, fileName.lastIndexOf('.'));
            
            Properties props = new Properties();
            props.load(new FileInputStream(files[i]));
            
            String widths = props.getProperty("widths");
            String charset = props.getProperty("charset");                        
            
            StringTokenizer tokenizer = new StringTokenizer(widths, ",");
            int chCount = tokenizer.countTokens();
            byte[] widthArray = new byte[chCount];
            char[] charArray = new char[chCount];
            
            int cnt = 0;
            while(tokenizer.hasMoreTokens()){
                widthArray[cnt] = Byte.parseByte(tokenizer.nextToken().trim());
                cnt ++;
            }
            
            tokenizer = new StringTokenizer(charset, ",");
            cnt = 0;
            while(tokenizer.hasMoreTokens()){
                charArray[cnt] = tokenizer.nextToken().trim().charAt(0);
                cnt ++;
            }
            
            widthsArray[i] = widthArray;
            charsArray[i] = charArray;
        }
        
        
        // write font data to file
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        
        dos.write(files.length);
        
        // write font names
        for(int i = 0; i < files.length; i++){
            dos.writeUTF(fontNames[i]);
        }
        
        // write font widths and chars
        for(int i = 0; i < files.length; i++){
            
            int chCount = widthsArray[i].length;
            dos.write(chCount);
            
            dos.write(widthsArray[i]);
            dos.writeChars(new String(charsArray[i]));
        }
        
        dos.flush();
        dos.close();
        
        FileOutputStream fout = new FileOutputStream(fontDataFile);
        fout.write(baos.toByteArray());
        fout.flush();
        fout.close();
        
        // write as constants
        baos = new ByteArrayOutputStream();
        PrintWriter pw = new PrintWriter(baos);
        
        pw.println("package view.font;\r\n");
        pw.println("public class Fonts { \r\n");
        
        for(int i = 0; i < fontNames.length; i++){
            
            String line = "\t" + "public static final byte " + 
                                fontNames[i].toUpperCase() + " = " + i + ";\r\n";
            
            pw.println(line);
        }
        
        pw.println("}");
        pw.flush();
        pw.close();
        
        fout = new FileOutputStream(constantsFile);
        fout.write(baos.toByteArray());
        fout.flush();
        fout.close();
    }
    
}
